using System;
using Webshop20.Application.Operations.Messages;
using Webshop20.Domain.Common.Exceptions;
using Webshop20.Domain.Customers.Services.Contracts;

namespace Webshop20.Application.Operations.Services
{

    public interface IApproveNewCustomerService : IService{}

    public class ApproveNewCustomerService : ServiceBase, IApproveNewCustomerService
    {
        private ICustomerApprovalService customerApprovalService;

        public ApproveNewCustomerService(ICustomerApprovalService customerApprovalService)
        {
            this.customerApprovalService = customerApprovalService;
        }

        public override ISystemMessage Handle(ISystemMessage request)
        {
            var approveNewCustomerRequest = request as ApproveNewCustomerRequest;
            try
            {
                customerApprovalService.ChangeApprovedCustomersToActiveState(approveNewCustomerRequest.RegistrationKey);
            }
            catch (DomainException ex)
            {
                HandleDomainException(ex.DomainMessage);
            }
            

            if(DomainReportsFailure())
            {
                return new ApproveNewCustomerResponse(domainMessage);
            }

            return new ApproveNewCustomerResponse(null);
        }
    }
}